﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.IO;
using NUnit.Framework;
using OsamesFrameworkCore.Configuration;
using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Tools;
using OsamesFrameworkCore.Xml.Types;

namespace TestOsamesFrameworkCore.Xml
{
    /// <summary>
    /// Tested xml file is in TestOsamesFrameworkCore\Data directory, and copied automatically on build.
    /// </summary>
    [TestFixture]
    public class TestLoadXmlConfigurationAndLookupInto : TestsBase
    {
        private XmlDictionaryContainer _xdcData;

        /// <summary>
        /// Custom setup.
        /// </summary>
        [SetUp]
        public override void Setup()
        {
            string strTestedXml = Path.Combine(@AppDomain.CurrentDomain.BaseDirectory,
                                               @"Data\TestOsamesConfiguration.xml");
            Assert.IsTrue(File.Exists(strTestedXml), "Test XML doesn't exist");
            CoreConfiguration.Instance.LoadCoreConfiguration(strTestedXml);

            _xdcData = CoreConfiguration.Instance.XdcOsamesCoreConfiguration;
        }

        [TearDown]
        public void TearDown()
        {

        }

        [Test]
        public void TestAddValue()
        {
            _xdcData.Add("sectionBase", "author", "toto");

            Assert.AreEqual("toto", _xdcData.FindValue("sectionBase", "author"));
        }

        [Test]
        public void TestFindValue()
        {
            Assert.AreEqual("mssql", _xdcData.FindValue("sectiondatabase", "engine"));

            Assert.AreEqual("mssql", ConfigurationTools.FindValue(_xdcData, "sectiondatabase", "engine"));

        }

        [Test]
        public void TestNumerics()
        {
            Assert.True(
                (bool)
                ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "bool", ReturnXmlNodeValue.AsBool));

            Assert.False(
                (bool)
                ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "wrongbool", ReturnXmlNodeValue.AsBool));

            Assert.AreEqual(52,
                            ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "int",
                                                                     ReturnXmlNodeValue.AsInt));

            Assert.AreEqual(4294967296,
                            ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "long",
                                                                     ReturnXmlNodeValue.AsLong));

            Assert.AreEqual(0,
                            ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "wronglong",
                                                                     ReturnXmlNodeValue.AsLong));

            Assert.AreEqual(0,
                            ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectiondata", "wrongdecimal",
                                                                     ReturnXmlNodeValue.AsDecimal));

            Assert.AreEqual(0,
                            ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectionbase", "nonexistant",
                                                                     ReturnXmlNodeValue.AsDecimal),
                            string.Format("Valeur de nonexistant: {0}",
                                          ConfigurationTools.FindParseXmlNodeValue(_xdcData, "sectionbase",
                                                                                   "nonexistant",
                                                                                   ReturnXmlNodeValue.AsDecimal)));

        }

        [Test]
        public void TestSetValue()
        {
            _xdcData.SetValue("sectionbase", "themename", "MonTheme");
            Assert.AreEqual("MonTheme", _xdcData.FindValue("sectionbase", "themename"));
        }

        [Test]
        [ExpectedException(typeof (XmlDictionaryException))]
        public void TestNonexistentSection()
        {
            try
            {
                string strResult = _xdcData.FindValue("nonexistantsection", "author");
                LoggerService.LogToOutput(strResult);
            }
            catch (Exception ex)
            {
                LoggerService.LogToOutput(ex.Message);
                // on pourrait faire un assert sur la valeur de ex.Message mais j'ai tout mis en internal ^^
                throw;
            }
        }

        [Test]
        [ExpectedException(typeof (XmlDictionaryException))]
        public void TestNonexistentTag()
        {
            try
            {
                string strResult = _xdcData.FindValue("sectionbase", "nonexistanttag");
                LoggerService.LogToOutput(strResult);
            }
            catch (Exception ex)
            {
                LoggerService.LogToOutput(ex.Message);
                // on pourrait faire un assert sur la valeur de ex.Message mais j'ai tout mis en internal ^^
                throw;
            }

        }
    }
}
